/**
 * Copyright 厦门中软海晟信息技术有限公司 版权所有 违者必究 2019
 */
package com.opencvjava.lessons.imgproc;

import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Random;

import static com.opencvjava.support.util.CvUtils.imshow;
import static org.opencv.core.CvType.CV_8UC3;
import static org.opencv.imgproc.Imgproc.*;
import static org.opencv.imgcodecs.Imgcodecs.*;
import static org.opencv.core.Core.*;

/**
 * @author : sunzb(sunzb@hsit.com.cn)
 * @date: 2019/1/4
 */
@Service
public class L2_RandomGeneratorAndText {
    private static final int WINDOW_WIDTH = 1024;
    private static final int WINDOW_HEIGHT = 768;
    private static final int LINE_TYPE = 8;
    private final Logger logger = LoggerFactory.getLogger(L2_RandomGeneratorAndText.class);

    public void test() {
        Random random = new Random();
        int iteTimes = random.nextInt(20);
        logger.warn("rand:{}", random.nextInt(100));
        Mat image = Mat.zeros(WINDOW_HEIGHT, WINDOW_WIDTH, CV_8UC3);
        Point pt1 = new Point();
        Point pt2 = new Point();
        for (int i = 0; i < iteTimes; i++) {
            pt1.x = random.nextInt(WINDOW_WIDTH + 1);
            pt1.y = random.nextInt(WINDOW_HEIGHT + 1);
            pt2.x = random.nextInt(WINDOW_WIDTH + 1);
            pt2.y = random.nextInt(WINDOW_HEIGHT + 1);
            line(image, pt1, pt2, new Scalar(random.nextInt(256)),
                    random.nextInt(10) + 1, LINE_TYPE);
        }
        imshow("随机直线", image);
        Point org = new Point();
        for (int i = 1; i < iteTimes; i++) {
            org.x = random.nextInt(WINDOW_WIDTH + 1);
            org.y = random.nextInt(WINDOW_HEIGHT + 1);
            putText(image, "Testing text rendering", org, random.nextInt(8),
                    random.nextInt(100) * 0.05 + 0.1, new Scalar(random.nextInt(256)),
                    random.nextInt(10) + 1, LINE_TYPE);
        }
        imshow("加随机文字", image);
        Mat image2 = new Mat(image.size(), image.type());
        int rows = image.rows();
        int cols = image.cols();
        int channels = image.channels();
        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                byte[] pixel = new byte[channels];
                double[] doubles = image.get(row, col);
                for (int channel = 0; channel < channels; channel++) {
                    pixel[channel] = (byte) (((int) (doubles[channel] - 50)) & 0xff);
                }
                image2.put(row, col, pixel);
            }
        }
        Size textSize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, new int[]{0});
        Point org1 = new Point((WINDOW_WIDTH - textSize.width) / 2,
                (WINDOW_HEIGHT - textSize.height) / 2);
        putText(image2, "OpenCV forever!", org1, FONT_HERSHEY_COMPLEX, 3,
                new Scalar(50, 50, 255), 5, LINE_TYPE);
        imshow("最终结果", image2);
    }
}
